var hxt = (function () {
  'use strict';

  /* eslint-disable import/no-mutable-exports */
  /* eslint-disable no-var */
  /* eslint-disable no-restricted-globals */
  var hxt = {};

  var isReady = false;

  function wxMiniProgramGetEnvCallback(res) {
    isReady = res.miniprogram;
    if (!isReady) {
      console.error('请在小程序 <web-view> 中使用 hxt-webview.js');
    }
  }

  function init(callback) {
    if (!wx || !wx.miniProgram) {
      console.error('请引入最新版本的微信 JSSDK');
      isReady = false;
    } else {
      wx.miniProgram.getEnv(wxMiniProgramGetEnvCallback);
    }
  }

  function invokeHxt(method, ...args) {
    if (isReady) {
      wx.miniProgram.postMessage({
        data: {
          type: 'invokeHxt',
          method,
          args,
          timestamp: Date.now(),
          url: location.href,
        },
      });
    }
  }

  hxt.identify = function invokeHxtIdentify(identData) {
    return invokeHxt('identify', identData);
  };

  hxt.sendAction = function invokeHxtSendAction(name, props) {
    return invokeHxt('sendAction', name, props);
  };

  hxt.sendPageview = function invokeHxtWebviewPageview(params = {}) {
    return invokeHxt('webviewPageview', params);
  };

  init();

  return hxt;

}());
